package utm.servlet.example;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import md.utm.simple_jdbc.MySQLAccess;
import md.utm.simple_jdbc.Person;

// Extend HttpServlet class
public class SavePersonToDatabase extends HttpServlet {
	MySQLAccess access;

	@Override
	public void init() throws ServletException {
		access = new MySQLAccess();
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Person person = (Person) request.getSession().getAttribute("person");
		response.sendRedirect("/utm-servlet-example/viewdata.jsp");
		access.insertRow(person.getName(), person.getSurname(),
				person.getYearOfBirth(), person.isStudent());
	}

	@Override
	public void destroy() {
		// do nothing.
	}
}